import { reqCartList,reqDeleteCartById,reqUpdateStateById } from "@/api"
const state = {
    cartList:{}
}
const mutations = {
    GETSHOPCART(state,cartList){
        state.cartList = cartList
    }
}
const actions = {
    //获取产品信息的action
    async getShopCart({commit}){
        let result = await reqCartList()
        if(result.code == 200){
            commit('GETSHOPCART',result.data)
        }
    },
    //通过商品id删除购物车商品
    async deleteCartById({commit},{skuId}){
        let result = await reqDeleteCartById(skuId)
    },
    //修改选中状态
    async updateStateById({commit},{skuId,isChecked}){
        let result = await reqUpdateStateById(skuId,isChecked)

    },

    //修改购物车全选的actions
    updateAllCartIsChecked({dispatch,state},isChecked){
        let promiseAll = []
        state.cartList[0].cartInfoList.forEach(item=>{
            let promise = dispatch('updateStateById',{skuId:item.skuId,isChecked})
            promiseAll.push(promise)
        })
        return Promise.all(promiseAll)
    }
}
const getters = {
    cartList(state){
        return state.cartList[0] || {}
    }
}
export default{
    state,
    mutations,
    actions,
    getters
}